package com.volunteer_sys.Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;

@Service
public class RedisUtils {

    @Autowired
    private RedisTemplate redisTemplate;

    public Integer getRestNum(Integer att_id) {
        Integer rs = 0;
        synchronized (this) {
            if (Boolean.TRUE.equals(redisTemplate.hasKey(att_id))) {
                try {
                    rs = (Integer) redisTemplate.boundHashOps(att_id).get(0);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else return 0;
        }
        return rs;
    }

    public boolean addStuCache(Integer att_id, Integer stu_id) {
        synchronized (this) {
            boolean rs = false;
            if (Boolean.TRUE.equals(redisTemplate.hasKey(att_id))) {
                Integer restnum = (Integer) redisTemplate.boundHashOps(att_id).get(0);
                if (restnum != null && restnum > 0) {
                    try {
                        Date date = new Date();
                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
                        redisTemplate.boundHashOps(att_id).put(stu_id, dateFormat.format(date));
                        restnum--;
                        redisTemplate.boundHashOps(att_id).put(0, restnum);
                        rs = true;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            return rs;
        }
    }

    public boolean removeStuCache(Integer att_id, Integer stu_id) {
        boolean rs = false;
        if (Boolean.TRUE.equals(redisTemplate.hasKey(att_id)) && Boolean.TRUE.equals(redisTemplate.boundHashOps(att_id).hasKey(stu_id))) {
            try {
                redisTemplate.boundHashOps(att_id).delete(stu_id);
                rs = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return rs;
    }

    //删除活动缓存
    public boolean removeAttCache(Integer att_id) {
        boolean rs = false;
        if (Boolean.TRUE.equals(redisTemplate.hasKey(att_id))) {
            try {
                redisTemplate.delete(att_id);
                rs = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return rs;
    }

    //增加活动缓存
    public boolean addAttCache(Integer att_id, Integer restnum) {
        boolean rs = false;
        if (!Boolean.TRUE.equals(redisTemplate.hasKey(att_id))) {
            try {
                redisTemplate.boundHashOps(att_id).put(0, restnum);
                rs = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return rs;
    }

    //指定活动id返回学生id的list
    public Set<Integer> getAllStuId(Integer att_id) {
        return redisTemplate.boundHashOps(att_id).keys();
    }

}

